<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
Term processing
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial031.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial033.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc66">4.9</A>&nbsp;&nbsp;Term processing</H2>
Apart from unification/matching, there are a number of generic built-in
predicates that work on arbitrary data terms.
The <CODE>=..</CODE> predicate converts structures into lists and vice versa:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- foo(a, b, c) =.. List.
List = [foo, a, b, c]
Yes (0.00s cpu)
?- Struct =.. [foo, a, b, c].
Struct = foo(a, b, c)
Yes (0.00s cpu)
</PRE></BLOCKQUOTE>
The arg/3 predicate extracts an argument from a structure:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- arg(2, foo(a, b, c), X).
X = b
Yes (0.00s cpu)
</PRE></BLOCKQUOTE>
The functor/3 predicate extracts functor name and arity from a structured term,
or, conversely, creates a structured term with a given functor name and arity:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- functor(foo(a, b, c), N, A).
N = foo
A = 3
Yes (0.00s cpu)
?- functor(F, foo, 3).
F = foo(_1696, _1697, _1698)
Yes (0.00s cpu)
</PRE></BLOCKQUOTE>
The term_variables/2 predicate extracts all variables from an arbitrarily
complex term:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- term_variables(foo(X, 3, Y, X), Vars).
Vars = [Y, X]
</PRE></BLOCKQUOTE>
The copy_term/2 predicate creates a copy of a term with fresh variables:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- copy_term(foo(3, X), Copy).
Copy = foo(3, _864)
Yes (0.00s cpu)
</PRE></BLOCKQUOTE>
<HR>
<A HREF="tutorial031.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial033.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
